<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller;

use app\common\controller\Admin;
use app\basic\controller\api\Hazard as HazardApi;
use app\admin\controller\ajax\Library as LibarayAjax;
use app\basic\controller\api\Detection;
/**
 * 危险源管理
 * @package app\rescue
 */
class Hazard  extends Admin

{
    
    protected $api;
    protected $property;
    protected $sourceType = 1;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new HazardApi();
		$this->property = new LibarayAjax();
	}
    /**
     * 首页
     * @return Json/
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$hazardList = $this->api->hazardList($this->scope);
   	  	return $hazardList;
   	  }
   	  $enterpriseList = model('Enterprise')->getSelectList($this->scope);
	  $this->assign('enterpriseList',$enterpriseList);
      return $this->fetch();
 	}
 	

 	/**
 	 * 危险源查询
 	 * @return Json/View
 	 */
 	public function search() {
 		if(IS_AJAX){
 			$hazardList = $this->api->search(input('keyword'),input('enterpriseId'));
 			return $hazardList;
 		}
 	}
 	
 	
 	/**
 	 * 添加危险源
 	 * @return Json/View
 	 */
 	public function add(){
 		if(IS_POST){
 			$data = input('post.');
 			if(isset($data['isValid'])){
 				$data['isValid'] = 1;
 			}else{
 				$data['isValid'] = 0;
 			}
 			return $this->api->add($data);
 		}
 		$enterpriseList = model('Enterprise')->getSelectList($this->scope);
 		$this->assign('enterpriseList',$enterpriseList);
 		$typeList = $this->property->getHazardType()->getData();
 		$this->assign('typeList',$typeList);
 		return $this->fetch();
 	}
 	
 	/**
 	 * 编辑危险源
 	 * @param string $hazardId
 	 * @return Json/View
 	 */
 	public function edit($hazardId = null){
 		if(IS_POST){
 			$data = input('post.');
 			if(isset($data['isValid'])){
 				$data['isValid'] = 1;
 			}else{
 				$data['isValid'] = 0;
 			}
 			return $this->api->edit($data,$hazardId);
 		}
 		$enterpriseList = model('Enterprise')->getSelectList($this->scope);
 		$this->assign('enterpriseList',$enterpriseList);
 		$typeList = $this->property->getHazardType()->getData();
 		$this->assign('typeList',$typeList);
 		$hazard = model('Hazard')->find($hazardId);
 		$this->assign('info',$hazard);
 		return $this->fetch();
 		
 	}
 	
 	/**
 	 * 删除危险源
 	 * @return Json
 	 */
 	public function del($hazardId = null) {
 		if(IS_AJAX){
 			return $this->api->del($hazardId);
 		}
 	}


	/**
	 * 设置危险源位置
	 * @param string $position
	 * @param string $enterprise
	 * @return mixed
	 */
	public function setBuilding($position = null,$hazardId = null){
		$center = $this->api->getHazardPosition($hazardId)->getData();
		$center = $center['code']?$center['data']:null;
		$this->assign('center',$center);
		$this->assign('position',$position);
		return $this->fetch();
	}
 	
 	/**
 	 * 配置传感器
 	 * @param string $sensors
 	 * @return view
 	 */
 	public function config($dangerSourceId = null,$sensors = null){
 		$detectionApi = new Detection();
 		$sensorList = $detectionApi->sensorList($dangerSourceId)->getData();
 		$this->assign('sensorList',$sensorList);
 		$this->assign('sensors',$sensors);
 		return $this->fetch();
 	}
 	
 	
 	public function audit($id = null){
 		return $this->api->audit($id);
 	}
}
